// 开始冒险吧
import 'package:flame/components.dart';
import 'package:flame/events.dart';
import 'package:flutter_flame/routes/RouteGame.dart';

import '../../../config/AssetsName.dart';
import '../../../component/ScaleSpriteComponent.dart';
import '../../game/GameWorld.dart';

// 开始冒险
class SelectorScreenStartAdventureComponent
    extends _SelectorScreenButtonComponent {
  SelectorScreenStartAdventureComponent()
      : super(
          position: Vector2(30, -260),
          originalButtonWidth: 380.0,
          hoverButtonOffset: Vector2(0, 40),
          buttonShadowSrc: AssetsName.spriteSelectorScreenShadowStartAdventure,
          originalButtonSrc:
              AssetsName.spriteSelectorScreenStartAdventureButton1,
          highlightButtonSrc:
              AssetsName.spriteSelectorScreenStartAdventureHighlight,
          shadowOffset: Vector2(-8, 1),
          shadowExpand: 20,
          router: GameWorld.routeName,
        );
}

class SelectorScreenSurvivalComponent extends _SelectorScreenButtonComponent {
  SelectorScreenSurvivalComponent()
      : super(
          position: Vector2(32, -140),
          originalButtonWidth: 359.0,
          hoverButtonOffset: Vector2(0, 40),
          buttonShadowSrc: AssetsName.spriteSelectorScreenShadowSurvival,
          originalButtonSrc: AssetsName.spriteSelectorScreenSurvivalButton,
          highlightButtonSrc: AssetsName.spriteSelectorScreenSurvivalHighlight,
          shadowOffset: Vector2(-8, 1),
          shadowExpand: 15,
          router: "",
        );
}

//
class SelectorScreenVaseBreakerComponent
    extends _SelectorScreenButtonComponent {
  SelectorScreenVaseBreakerComponent()
      : super(
          position: Vector2(34, 34),
          originalButtonWidth: 310.0,
          hoverButtonOffset: Vector2(0, 35),
          buttonShadowSrc: AssetsName.spriteSelectorScreenShadowZenGarden,
          originalButtonSrc: AssetsName.spriteSelectorScreenVaseBreakerButton,
          highlightButtonSrc:
              AssetsName.spriteSelectorScreenVaseBreakerHighlight,
          shadowOffset: Vector2(-8, 1),
          shadowExpand: 15,
          router: "",
        );
}

class SelectorScreenChallengesComponent extends _SelectorScreenButtonComponent {
  SelectorScreenChallengesComponent()
      : super(
          position: Vector2(36, -45),
          originalButtonWidth: 326.0,
          hoverButtonOffset: Vector2(0, 35),
          buttonShadowSrc: AssetsName.spriteSelectorScreenShadowChallenge,
          originalButtonSrc: AssetsName.spriteSelectorScreenChallengesButton,
          highlightButtonSrc:
              AssetsName.spriteSelectorScreenChallengesHighlight,
          shadowOffset: Vector2(-8, 1),
          shadowExpand: 15,
          router: "",
        );
}

class _SelectorScreenButtonComponent extends PositionComponent {
  final double originalButtonWidth; // 按钮宽度
  final Vector2 hoverButtonOffset; // 偏移
  final String buttonShadowSrc; // 阴影
  final String originalButtonSrc; // 按钮原本图像
  final String highlightButtonSrc; // 高亮按钮图像
  final double shadowExpand; // 阴影扩大的数值
  final Vector2 shadowOffset; // 阴影偏移
  final String router;

  _SelectorScreenButtonComponent({
    required super.position,
    required this.originalButtonWidth,
    required this.hoverButtonOffset,
    required this.buttonShadowSrc,
    required this.originalButtonSrc,
    required this.highlightButtonSrc,
    required this.shadowOffset,
    required this.shadowExpand,
    required this.router,
  });

  @override
  Future<void> onLoad() async {
    add(
      _SelectorScreenButtonShadow(
        originalButtonWidth,
        buttonShadowSrc,
        shadowOffset,
        shadowExpand,
      ),
    );
    add(
      _SelectorScreenButton(
        originalButtonWidth,
        originalButtonSrc,
        hoverButtonOffset,
        highlightButtonSrc,
        router,
      ),
    );
  }
}

class _SelectorScreenButtonShadow extends ScaleSpriteComponent {
  _SelectorScreenButtonShadow(
    double myWidth,
    String image,
    Vector2 shadowOffset,
    double shadowExpand,
  ) : super(myWidth + shadowExpand, shadowOffset, image);
}

class _SelectorScreenButton extends ScaleSpriteComponent {
  final Vector2 offset;
  final double myWidth;
  final String highlight;
  final String originalImageSrc;
  final String router;

  _SelectorScreenButton(
    this.myWidth,
    this.originalImageSrc,
    this.offset,
    this.highlight,
    this.router,
  ) : super(myWidth, Vector2.zero(), originalImageSrc);

  @override
  Future<void> onLoad() {
    add(
      _SelectorScreenButtonHover(
        offset,
        myWidth,
        highlight,
        originalImageSrc,
        router,
      ),
    );
    return super.onLoad();
  }
}

class _SelectorScreenButtonHover extends ScaleSpriteComponent
    with HoverCallbacks, TapCallbacks, HasGameReference<RouterGame> {
  Vector2 offset;
  double myWidth;
  String highlightImageSrc;
  String originalImageSrc;
  String router;

  late final highlight = _SelectorScreenButtonHighlight(
    offset,
    myWidth,
    highlightImageSrc,
  );

  _SelectorScreenButtonHover(
    this.offset,
    this.myWidth,
    this.highlightImageSrc,
    this.originalImageSrc,
    this.router,
  ) : super(
          myWidth,
          offset,
          originalImageSrc,
          tOpacity: 0,
          scaleFit: false,
          fixedSize: Vector2(myWidth, 80),
        );

  @override
  void onHoverEnter() {
    add(highlight);
    super.onHoverEnter();
  }

  @override
  void onHoverExit() {
    if (highlight.parent != null) remove(highlight);
    super.onHoverExit();
  }

  @override
  void onTapDown(TapDownEvent event) {
    add(highlight);
    super.onTapDown(event);
  }

  @override
  void onTapCancel(TapCancelEvent event) {
    remove(highlight);
    super.onTapCancel(event);
  }

  @override
  void onTapUp(TapUpEvent event) {
    if (router.isNotEmpty) {
      game.router.pushNamed(router);
      remove(highlight);
    }
    super.onTapUp(event);
  }
}

class _SelectorScreenButtonHighlight extends ScaleSpriteComponent {
  final Vector2 offset;
  final double myWidth;

  _SelectorScreenButtonHighlight(
    this.offset,
    this.myWidth,
    String highlightImageSrc,
  ) : super(myWidth, -offset, highlightImageSrc);
}
